/****************************************************************************/
/*                                                                          */
/*   FILE:    tranfifo.c	                                                */
/*   DATE:    13/06/97                                                      */
/*   PURPOSE: DVB-CI Transport layer - spdu FIFO management for TRANS task  */  
/*		there is a fifo by transport connection								*/
/*		The SPDU are not copied, only the pointer is copied					*/
/*   TAB STOPS : 4															*/
/*   HISTORY:				                                 			    */
/*   Version    Date  		    Written by	     Notes	    	    		*/
/*	 1.0	    13/06/1997      SCM              Initial implementation 	*/
/*   1.05       28/12/1998      SCM              Renamed function:          */
/*                                     TRPT_RazFifo() into TRPT_ResetFifo() */
/*                                                                    	    */
/*--------------------------------------------------------------------------*/
/*   GENERAL COMMENTS                                                 	    */
/*																		    */
/****************************************************************************/
#include <stdio.h>
#include <memory.h>
#include "sm_trace.h"
#include "xmsg.h"
#include "itc.h"
#include "trans.h"

/*--------------------------------------------------------------------------*/
/* External Variables		                                            	*/
/*--------------------------------------------------------------------------*/
extern trn_fifo_t g_trn_tab_fifo[];	/* FIFO table									*/


/* ======================================================================== */
/* NAME : TRPT_ResetFifo					 								*/
/* 									    									*/
/* DESCRIPTION : clear the fifo associated with the transport connection	*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*		trnTcidIndex :		instance of the current state diagram and for the fifo	*/
/*									 										*/
/* OUTPUT PARAMETERS :							   							*/
/*									    									*/
/* RETURN CODE								    							*/
/*									  										*/
/* ======================================================================== */
void TRPT_ResetFifo(int trnTcidIndex)
{  
	unsigned char tag;	/* return code from TRPT_GetFifo */
	tran_spdu_t ptr;			/* fifo entry					 */

	SM_TRACE_ENTRY(TRMOD_TRANS, "TRPT_ResetFifo() - entry,trnTcidIndex=%d\r\n", trnTcidIndex);

	if (g_trn_tab_fifo[trnTcidIndex].m_count)
	{
		SM_TRACE_ERROR(TRMOD_TRANS, "TRPT_ResetFifo() - fifo not empty,count=%d,clear it\r\n",
					g_trn_tab_fifo[trnTcidIndex].m_count);
		/* fifo not empty : delete the entries and the allocated memory */
		do
		{
			tag = TRPT_GetFifo(trnTcidIndex, &ptr);
			if (tag == SPDU)
				ITC_DynFree(ptr.m_pbSpdu);		/* free the allocated memory */
		} 
		while (tag);
	}
	
	g_trn_tab_fifo[trnTcidIndex].m_head  = 0;
	g_trn_tab_fifo[trnTcidIndex].m_tail  = 0;     
	g_trn_tab_fifo[trnTcidIndex].m_count = 0;
	/* the fifo is empty */
	
	SM_TRACE_RETURN(TRMOD_TRANS, "TRPT_ResetFifo() - return\r\n");
	return;
}
/* ======================================================================== */
/* NAME : TRPT_PutFifo					 									*/
/* 									    									*/
/* DESCRIPTION : Put a SPDU message in fifo		    						*/
/*		trnTcidIndex :		instance of the current state diagram and for the fifo	*/  
/*		tag :		SPDU or SS_DELETE_TC									*/
/*		ptr :		SPDU to put in fifo	if tag = SPDU						*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*									 										*/
/* OUTPUT PARAMETERS :							   							*/
/*									    									*/
/* RETURN CODE								    							*/
/*		1 Success															*/
/*		0 Error																*/
/*									  										*/
/* ======================================================================== */
int TRPT_PutFifo(int trnTcidIndex, unsigned char tag, tran_spdu_t *ptr)
{
	SM_TRACE_ENTRY(TRMOD_TRANS, "TRPT_PutFifo() - entry,trnTcidIndex=%d,tag=%d,ptr=0x%08X\r\n", trnTcidIndex,tag,ptr);

	if (g_trn_tab_fifo[trnTcidIndex].m_count >= TRN_MAXFIFOELT)
	{
		SM_TRACE_ERROR(TRMOD_TRANS, "TRPT_PutFifo() - fifo full,count=%d\r\n",
					g_trn_tab_fifo[trnTcidIndex].m_count);
		return 0;		/* full fifo : Error */
	}

	SM_TRACE_BYTES(TRMOD_TRANS, TRLEV_NOTIFY, ptr->m_pbSpdu, ptr->m_len, 
				"TRPT_PutFifo() - put spdu[tag=%s(0x%02X) tcid=%d head=%d tail=%d]:", 
				XMSG_NAME(tag), tag, ptr->m_tcid, 
				g_trn_tab_fifo[trnTcidIndex].m_head,	g_trn_tab_fifo[trnTcidIndex].m_tail);

	g_trn_tab_fifo[trnTcidIndex].m_count++;	/* one more element */

    /* element's copy in fifo */
    g_trn_tab_fifo[trnTcidIndex].m_tag[g_trn_tab_fifo[trnTcidIndex].m_tail] = tag;
    if (tag == SPDU)
	{
		g_trn_tab_fifo[trnTcidIndex].m_arItems[g_trn_tab_fifo[trnTcidIndex].m_tail].m_tcid  = ptr->m_tcid;
		g_trn_tab_fifo[trnTcidIndex].m_arItems[g_trn_tab_fifo[trnTcidIndex].m_tail].m_pbSpdu = ptr->m_pbSpdu;
		g_trn_tab_fifo[trnTcidIndex].m_arItems[g_trn_tab_fifo[trnTcidIndex].m_tail].m_len   = ptr->m_len;
	}

	g_trn_tab_fifo[trnTcidIndex].m_tail++;	/* tail is updated  */
	if (g_trn_tab_fifo[trnTcidIndex].m_tail >= TRN_MAXFIFOELT)
		g_trn_tab_fifo[trnTcidIndex].m_tail = 0;	
	
	SM_TRACE_RETURN(TRMOD_TRANS, "TRPT_PutFifo() - return 1\r\n");
	return 1;	/* Success */					
}

/* ======================================================================== */
/* NAME : TRPT_GetFifo					 									*/
/* 									    									*/
/* DESCRIPTION : Get a SPDU saved in fifo	    							*/
/* 									    									*/
/* INPUT PARAMETERS :							    						*/
/*		trnTcidIndex :		instance of the current state diagram and for the fifo	*/  
/*									 										*/
/* OUTPUT PARAMETERS :							   							*/
/*		ptr :		SPDU get out the fifo									*/
/*									    									*/
/* RETURN CODE								    							*/
/*		>0 Success : Tag (SPDU or SS_DELETE_TC)								*/
/*		0 Error	(empty)														*/
/*									  										*/
/* ======================================================================== */
unsigned char TRPT_GetFifo(int trnTcidIndex, tran_spdu_t *ptr)
{
	unsigned char tag;	/* SS_DELETE_TC or SPDU */

	SM_TRACE_ENTRY(TRMOD_TRANS, "TRPT_GetFifo() - entry,trnTcidIndex=%d,ptr=0x%08X\r\n", trnTcidIndex,ptr);

	if (g_trn_tab_fifo[trnTcidIndex].m_count <= 0)
	{
		SM_TRACE_INFO(TRMOD_TRANS, "TRPT_GetFifo() - fifo empty\r\n");
		return 0;		/* empty : Error */
	}
		
	g_trn_tab_fifo[trnTcidIndex].m_count--;	
    
    /* element's copy out fifo */
	tag = g_trn_tab_fifo[trnTcidIndex].m_tag[g_trn_tab_fifo[trnTcidIndex].m_head];
	if (tag == SPDU)
	{
		ptr->m_tcid  = g_trn_tab_fifo[trnTcidIndex].m_arItems[g_trn_tab_fifo[trnTcidIndex].m_head].m_tcid;
		ptr->m_pbSpdu = g_trn_tab_fifo[trnTcidIndex].m_arItems[g_trn_tab_fifo[trnTcidIndex].m_head].m_pbSpdu;
		ptr->m_len   = g_trn_tab_fifo[trnTcidIndex].m_arItems[g_trn_tab_fifo[trnTcidIndex].m_head].m_len;
	}

	SM_TRACE_BYTES(TRMOD_TRANS, TRLEV_NOTIFY, ptr->m_pbSpdu, ptr->m_len, 
				"TRPT_GetFifo() - get spdu[tag=%s(0x%02X) tcid=%d head=%d tail=%d]:", 
				XMSG_NAME(tag), tag, ptr->m_tcid, 
				g_trn_tab_fifo[trnTcidIndex].m_head,	g_trn_tab_fifo[trnTcidIndex].m_tail);

	g_trn_tab_fifo[trnTcidIndex].m_head++;	/* head is updated  */
	if (g_trn_tab_fifo[trnTcidIndex].m_head >= TRN_MAXFIFOELT)
		g_trn_tab_fifo[trnTcidIndex].m_head = 0;	
	
	SM_TRACE_RETURN(TRMOD_TRANS, "TRPT_GetFifo() - return, tag=%s(%d)\r\n", XMSG_NAME(tag), tag);
	return tag;
	/* Success */					
}
